<%@page import="com.grom.service.GcmsUserService"%>
<%@page import="com.grom.util.Constants"%>
<%@page import="com.grom.po.GcmsUser"%>
<%@page import="com.grom.util.SecurityUtil"%>
<%@page import="java.util.Map"%>
<%@page import="com.grom.po.Customer"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/icon.css">
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.6.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.easyui.min.js"></script>
<script type="text/javascript">
var currentUserId = "";
<%
	if(session.getAttribute("USER")!=null&&session.getAttribute("USER").equals("ADMIN")){
		%>
		currentUserId="ADMIN";
		<%
	}
	if(session.getAttribute("USER")!=null&&session.getAttribute(Constants.KEY_USER_SESSION)!=null){
		%>
		currentUserId="<%=((GcmsUser)session.getAttribute(Constants.KEY_USER_SESSION)).getID() %>";
		<%
	}
%>
$(function(){
	$('#loginInfoEdit').dialog({  
	    title : 'Customer Info',  
	    modal: true,  
	    collapsible : true,    
	    resizable : true   
	}); 
	$('#loginInfoEdit').dialog('close');
	<% if (request.getAttribute("cusType_s")!=null) {%>
	$('#cusType_s').val('<%=request.getAttribute("cusType_s") %>');
	<% } %>
	<% if (request.getAttribute("customerName_s")!=null) {%>
	$('#customerName_s').val('<%=request.getAttribute("customerName_s") %>');
	<% } %>
	<% if (request.getAttribute("pageNum")!=null) {%>
	$('#pageNum').val('<%=request.getAttribute("pageNum") %>');
	<% } %>
	$('#win').window({  
	    width:600,  
	    height:400,  
	    modal:true  
	});  
	$('#win').window('close');  // close window
});


function showAddDialog(){
	clearTheContactInfoAndHistory();
	$('input').each(function(){
		$(this).val('');
	});
	$('select').each(function(){
		$(this).val('');
	});
	$('#save').linkbutton('enable');
	$('#save1').linkbutton('enable');
	$('#loginInfoEdit').dialog('open');
}

function loginInfoEdit(){
	var canSub = true;
	$('input').each(function(){
		if($(this).attr("required")=="true"){
			if($(this).val()==""){
				$(this).focus();
				canSub =false;
				return;
			}
		}
	});
	$('select').each(function(){
		if($(this).attr("required")=="true"){
			if($(this).val()==""){
				$(this).focus();
				canSub =false;
				return;
			}
		}
	});
	if(canSub){
		$('#cusForm').submit();
	}
}
function viewDetail(){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	$.ajax({
	    url: '<%=request.getContextPath()%>/CustomerAction/getDetail.do?itemid='+itemId,
	    type: 'GET',
	    timeout: 1000,
	    error: function(){
	        alert('Error getting response!');
	    },
	    success: function(json){
	        var customer = eval(json);
	        $('#ID').val(customer.ID);
	        $('#cusType').val(customer.cusType);
	        $('#brandName').val(customer.brandName);
	        $('#visitedAddress').val(customer.visitedAddress);
	        $('#customerName').val(customer.customerName);
	        $('#averageSale').val(customer.averageSale);
	        $('#storeNum').val(customer.storeNum);
	        $('#monthlyCoverNum').val(customer.monthlyCoverNum);
	        $('#monthlyCoverDay').val(customer.monthlyCoverDay);
	        $('#cusFlowCover').val(customer.cusFlowCover);
	        $('#cusFlowCoverWay').val(customer.cusFlowCoverWay);
	        $('#distanceStoreCover').val(customer.distanceStoreCover);
	        $('#distanceStoreCoverWay').val(customer.distanceStoreCoverWay);
	        $('#everUsedFlowCover').val(customer.everUsedFlowCover);
	        $('#everUsedDistanceStoreCover').val(customer.everUsedDistanceStoreCover);
	        $('#reasonOfNotUsed').val(customer.reasonOfNotUsed);
	        $('#wantUse').val(customer.wantUse);
	        $('#reasonOfRefuse').val(customer.reasonOfRefuse);
	        $('#importantFunction').val(customer.importantFunction);
	        $('#chargeWayWanted').val(customer.chargeWayWanted);
	        $('#cusFlowChargeWanted').val(customer.cusFlowChargeWanted);
	        $('#storeCoverChargeWanted').val(customer.storeCoverChargeWanted);
	        $('#userId').val(customer.userId);
	        $('#webSite').val(customer.webSite);
	        $('#flowCoverType').val(customer.flowCoverType);
	        $('#inforSource').val(customer.inforSource);
	        clearTheContactInfoAndHistory();
	        for(var i = 0 ; i < customer.contactInfo.length ; i++){
	        	var html="";
	        	html += customer.contactInfo[i].contactPerson + "-" + customer.contactInfo[i].contactPhone;
	        	if($('#contactInfo').val()==""){
					$('#contactInfoDiv').html("<span id='"+html+"'>"+html+"&nbsp;<a href='#' onclick=\"removeFromContact('"+html+"')\">remove</a></span>");
					$('#contactInfo').val(html);
				}else{
					$('#contactInfoDiv').append("<br><span id='"+html+"'>"+html+"&nbsp;<a href='#' onclick=\"removeFromContact('"+html+"')\">remove</a></span>");
					$('#contactInfo').val($('#contactInfo').val()+","+ html);
				}
	        }
	        for(var i=0 ; i < customer.historys.length; i++){
	        	$('#historyInfo').val($('#historyInfo').val()+"-"+customer.historys[i].detailInfo);
		        $('#hisToryInfoDiv').append("<p>"+customer.historys[i].detailInfo+"</p>");
	        }
	        if(currentUserId == "ADMIN"||customer.userId == currentUserId ||customer.userId==""){
	        	$('#save').linkbutton('enable');
	        	$('#save1').linkbutton('enable');
	        	$('#save2').linkbutton('enable');
	        }else{
	        	$('#save').linkbutton('disable');
	        	$('#save1').linkbutton('disable');
	        	$('#save2').linkbutton('disable');
	        }
			$('#loginInfoEdit').dialog('open');
	    }
	});
}

function clearTheContactInfoAndHistory(){
	$('#contactInfo').val("");
	$('#contactInfoDiv').html("");
}

function doDelete(){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	if (itemId){  
        $.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){  
            if (r){  
				$('#deleteId').val(itemId);
				$('#deleteForm').submit();
            }  
        });  
    } 
}

function doSearch(num){
	if($('#pageNum').val() * 1 + num * 1 == 0){
		alert("已经到达最前页！");
		return;
	}
	if(num==0){
		$('#pageNum').val(1);
	}
	$('#pageNum').val($('#pageNum').val() * 1 + num * 1);
	$('#searchForm').submit();
}

function addContact(){
	$.messager.prompt('联系人','请填写姓名：', function(val){
		var html = val;
		$.messager.prompt('联系电话','请填写电话：', function(val){
			html += "-"+val;
			if($('#contactInfo').val()==""){
				$('#contactInfoDiv').html("<span id='"+html+"'>"+html+"&nbsp;<a href='#' onclick=\"removeFromContact('"+html+"')\">remove</a></span>");
				$('#contactInfo').val(html);
			}else{
				$('#contactInfoDiv').append("<br><span id='"+html+"'>"+html+"&nbsp;<a href='#' onclick=\"removeFromContact('"+html+"')\">remove</a></span>");
				$('#contactInfo').val($('#contactInfo').val()+","+ html);
			}
		});
	});
}

function removeFromContact(place){
	var contactInfo = $('#contactInfo').val();
	if(place == contactInfo){
		contactInfo="";
	}else{
		contactInfo = contactInfo.replace(","+place, "");
	}
	$('#contactInfo').val(contactInfo);
	$('#'+place).html("");
}

function showHistoryWin(){
	$('#history').val("add your history here!");
	$('#win').window('open'); 
}

function addHistory(){
	if($('#historyInfo').val()==""){
		$('#historyInfo').val($('#history').val());
	}else{
		$('#historyInfo').val($('#historyInfo').val()+ "-" +$('#history').val());
	}
	$('#hisToryInfoDiv').append("<p>"+$('#history').val()+"</p>");
	$('#win').window('close'); 
}
</script>
</head>
<body>
<div id="win" iconCls="icon-save" title="增加拜访记录">  
  	请在下面填写拜访记录：<br>
   	<textarea id="history" name="history" rows="15" cols="70" ></textarea>
   	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addHistory()">Add</a> 
</div>  
<div style="display: none;">
	<form name="deleteForm" id="deleteForm" action="<%=request.getContextPath() %>/CustomerAction/doDelete.do">
		<input type="text" name="deleteId" id="deleteId"/>
	</form>
</div>
<div style="font-size: 10px;">
	<form id="searchForm" name="searchForm" action="<%=request.getContextPath() %>/CustomerAction/conditionSelect.do" method="post">
      <label>客户分类：</label>  
      	<select name="cusType_s" id="cusType_s" style="width: 70px;">
      		<option value="">无</option>
 			<option value="A">A</option>
			<option value="B">B</option>
			<option value="C">C</option>
			<option value="D">D</option>
		</select>
      <label>公司名称：</label>  
      <input type="text" name="customerName_s" id="customerName_s" ></input>  
        <input type="hidden" id="pageNum" name="pageNum" value="1"/>
      <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="doSearch(0)">search</a>
    </form>
</div>
<table id="tt" class="easyui-datagrid"   iconCls="icon-edit"
        title="DataGrid with Toolbar" singleSelect="true"
        toolbar="#tb" >  
    <thead>  
        <tr>  
            <th field="productid" width="80">客户分类</th>  
            <th field="listprice" width="80" >公司名称</th>  
            <th field="unitcost" width="80" >公司地址</th>  
            <th field="attr1" width="150">公司网站</th>  
            <th field="keliu" width="50">是否安装客流统计</th> 
            <th field="sales" width="80">销售人员</th>  
            <th field="remark" width="150">其他</th>  
            <th hidden="true" field="itemid" width="40">Item ID</th>  
        </tr>  
    </thead>  
    <tbody>     
    	<%
    		List<Customer> allCustomer = (List<Customer>)request.getAttribute("allCustomer");
    		for(Customer customer : allCustomer){
    	%>
        <tr >                             
            <td><%=customer.getCusType() %></td>              
            <td><%=customer.getCustomerName() %></td>              
            <td><%=customer.getVisitedAddress() %></td>              
            <td><%=customer.getWebSite() %></td>  
            <td><%=customer.getEverUsedFlowCover() %></td>            
            <td><%=GcmsUserService.instance.getUserByID(customer.getUserId()).getUserName() %></td>  
            <td>请点击view查看详细</td>         
            <td><%=customer.getID() %></td>              
        </tr>  
    	<%
    		}
    	%>                         
    </tbody>
</table>  
<div id="tb">  
<%
	if(session.getAttribute("USER")!=null){
%>
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="showAddDialog()">Add</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true" onclick="viewDetail()">view/update</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="doDelete()">Delete</a> 
    <a href="#" class="easyui-linkbutton" iconCls="icon-back" plain="true" onclick="doSearch(-1)">上一页</a> 
	当前页数：<%=request.getAttribute("pageNum")==null?1:request.getAttribute("pageNum") %>
    <a href="#" class="easyui-linkbutton" iconCls="icon-back" plain="true" onclick="doSearch(1)">下一页</a> 
<%
	}
%>
</div>  


<!-- 编辑 -->  
<div id="loginInfoEdit"  
    style="padding: 5px; width: 600px; height: 400px;">  
    <h5 id="loginInfoEdit_message" style="color: red;"></h5>  
    <div class="ToolTip_Form" id="table_loginInfoEdit" onkeydown="if(event.keyCode==13){loginInfoEdit();}">  
       <form id="cusForm" name="cusForm" action="<%=request.getContextPath() %>/CustomerAction/doSave.do" method="post">
       <input type="text" name="ID" id="ID" value="" style="display: none"/>
       <ul>    
            <li>  
                <label>客户分类：</label>  
                      <select name="cusType" id="cusType" style="width: 40px;">
				      		<option value="A">A</option>
				      		<option value="B">B</option>
				      		<option value="C">C</option>
				      		<option value="D">D</option>
				      </select>
            </li>  
            <li>  
                <label>公司名称：</label>  
                <input type="text" class="easyui-validatebox" name="customerName" id="customerName" required="true" style="width: 500px;"></input>  
            </li>  
            <li>  
                <label>公司地址：</label>  
                <input type="text" class="easyui-validatebox" name="visitedAddress" id="visitedAddress" required="true" style="width: 500px;"></input>  
            </li> 
            <li>  
                <label>公司网站：</label>  
                <input type="text" class="easyui-validatebox" name="webSite" id="webSite" style="width: 500px;"></input>  
            </li> 
            <li>  
                <label>公司联系人公司联系方式增加：</label>  
                <a id="save1" name="save1" href="#" class="easyui-linkbutton" onclick="addContact()">增加</a>
                <input type="hidden" name="contactInfo" id="contactInfo"/>
                <div id="contactInfoDiv">
                </div>
            </li> 
            <li>  
                <label>是否使用过客流统计：</label>  
                <input type="text" class="easyui-validatebox" name="everUsedFlowCover" id="everUsedFlowCover" required="true"></input>  
            </li> 
           <li>  
                <label>客流统计型号：</label>  
                <input type="text" class="easyui-validatebox" name="flowCoverType" id="flowCoverType" ></input>  
            </li> 
            <li>  
                <label>拜访记录：</label>  
                <a id="save2" name="save2" href="#" class="easyui-linkbutton" onclick="showHistoryWin()">增加</a>
                <input type="hidden" name="historyInfo" id="historyInfo"/>
                <div id="hisToryInfoDiv">
                </div>
            </li>
            <li>  
                <label>公司信息来源：</label>  
                <input type="text" class="easyui-validatebox" name="inforSource" id="inforSource" required="true" style="width: 500px;"></input>  
            </li> 
           <li>  
                <label>销售人员：</label>  
                <select id="userId" name="userId"  required="true">
                	<option value=""></option>
                	<%
                	List<GcmsUser> users = SecurityUtil.getAllUser();
                		for(GcmsUser user : users){
                			%>
                			<option value="<%=user.getID() %>"><%=user.getUserName() %></option>
                			<%
                		}
                	%>
                </select>
            </li> 
            <li>  
            	<a id="save" name="save" href="#" class="easyui-linkbutton" onclick="loginInfoEdit();">保存</a>
            	<a href="#" class="easyui-linkbutton" onclick="$('#loginInfoEdit').dialog('close');">放弃操作</a>
            </li>  
        </ul>  
       </form>
    </div>  
</div>  
</body>
</html>
            <!-- 
            <li>  
                <label>品牌名称：</label>  
                <input type="text" class="easyui-validatebox" name="brandName" id="brandName" required="true"></input>  
            </li> 
            <li>  
                <label>店铺每月平均销售额：</label>  
                <input type="text" class="easyui-validatebox" name="averageSale" id="averageSale" required_d="true"></input>  
            </li> 
            <li>  
                <label>管理/经营店铺数量：</label>  
                <input type="text" class="easyui-validatebox" name="storeNum" id="storeNum" required_d="true"></input>  
            </li> 
            <li>  
                <label>每月巡店数量：</label>  
                <input type="text" class="easyui-validatebox" name="monthlyCoverNum" id="monthlyCoverNum" required_d="true"></input>  
            </li> 
            <li>  
                <label>每月巡店天数：</label>  
                <input type="text" class="easyui-validatebox" name="monthlyCoverDay" id="monthlyCoverDay" required_d="true"></input>  
            </li> 
            <li>  
                <label>是否知道客流统计：</label>  
                <input type="text" class="easyui-validatebox" name="cusFlowCover" id="cusFlowCover" required_d="true"></input>  
            </li> 
            <li>  
                <label>通过何种方式知道：</label>  
                <input type="text" class="easyui-validatebox" name="cusFlowCoverWay" id="cusFlowCoverWay" required_d="true"></input>  
            </li> 
            <li>  
                <label>是否知道远程巡店：</label>  
                <input type="text" class="easyui-validatebox" name="distanceStoreCover" id="distanceStoreCover" required_d="true"></input>  
            </li> 
            <li>  
                <label>通过何种渠道知道：</label>  
                <input type="text" class="easyui-validatebox" name="distanceStoreCoverWay" id="distanceStoreCoverWay" required_d="true"></input>  
            </li> 
            <li>  
                <label>是否使用过远程巡店：</label>  
                <input type="text" class="easyui-validatebox" name="everUsedDistanceStoreCover" id="everUsedDistanceStoreCover" required_d="true"></input>  
            </li> 
            <li>  
                <label>没有使用的原因：</label>  
                <input type="text" class="easyui-validatebox" name="reasonOfNotUsed" id="reasonOfNotUsed" required_d="true"></input>  
            </li> 
            <li>  
                <label>近期是否想使用：</label>  
                <input type="text" class="easyui-validatebox" name="wantUse" id="wantUse" required_d="true"></input>  
            </li> 
            <li>  
                <label>不想使用的原因：</label>  
                <input type="text" class="easyui-validatebox" name="reasonOfRefuse" id="reasonOfRefuse" required_d="true"></input>  
            </li> 
            <li>  
                <label>何种功能最重要：</label>  
                <input type="text" class="easyui-validatebox" name="importantFunction" id="importantFunction" required_d="true"></input>  
            </li> 
            <li>  
                <label>希望客流统计收费方式：</label>  
                <input type="text" class="easyui-validatebox" name="chargeWayWanted" id="chargeWayWanted" required_d="true"></input>  
            </li> 
            <li>  
                <label>合理的客流统计购买价格：</label>  
                <input type="text" class="easyui-validatebox" name="cusFlowChargeWanted" id="cusFlowChargeWanted" required_d="true"></input>  
            </li> 
             <li>  
                <label>合理的视频巡店购买价格：</label>  
                <input type="text" class="easyui-validatebox" name="storeCoverChargeWanted" id="storeCoverChargeWanted" required_d="true"></input>  
            </li> 
			 -->